package com.wang.music.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.music.config.MinioPropertites;
import com.wang.music.config.MinioTemplate;
import com.wang.music.dao.ConsumerMapper;
import com.wang.music.entity.Consumer;
import com.wang.music.service.ConsumerService;
import com.wang.music.util.DateUtil;
import com.wang.music.util.FileUtils;
import com.wang.music.util.PageUtils;
import com.wang.music.util.ResMap;
import com.wang.music.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端用户表 服务实现类
 * </p>
 *
 * @author zifan
 * @since 2021-03-10
 */
@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer> implements ConsumerService {

    @Autowired
    private MinioPropertites minioPropertites;

    @Override
    public Map<String, Object> uploadPic(MultipartFile pic) {
        HashMap<String, Object> resMap = new HashMap<>();
        MinioTemplate minioTemplate = new MinioTemplate(FileUtils.getMinioClient(minioPropertites));
        //对文件命做处理
        String filename = DateUtil.FormatDateForUpload(pic.getOriginalFilename());
        String bucket = minioPropertites.getBucket();
        try {
            minioTemplate.putObject(bucket,filename,pic.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回的url时页面上显示的url地址
        resMap.put("url",minioPropertites.getUrl() + "/" + bucket + filename);
        resMap.put("name",filename);
        return resMap;
    }

    @Override
    public Map<String, Object> addConsumer(Consumer consumer) {
        //todo 新增时应判断手机号，用户名是否被占用，这里不做了
        Map<String, Object> resMap = ResMap.OK();
        Date now = new Date();
        consumer.setCreateTime(now);
        consumer.setUpdateTime(now);
        this.save(consumer);
        resMap.put("msg", "保存成功");
        return resMap;
    }

    @Override
    public Map<String, Object> getConsumers(String key, PageVO pageVO) {
        Map<String, Object> resMap = ResMap.OK();
        Page<Consumer> page = PageUtils.toMpPage(pageVO);
        QueryWrapper<Consumer> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(key)) {
            wrapper.and(w -> {
                w.lambda().like(Consumer::getUsername, key).or().eq(Consumer::getPhoneNum, key);
            });
        }
        this.page(page, wrapper);
        resMap.put("page", PageUtils.toPageVO(page));
        return resMap;
    }

    @Override
    public Map<String, Object> updateConsumer(Consumer consumer) {
        Map<String, Object> resMap = ResMap.OK();
        this.updateById(consumer);
        resMap.put("msg", "修改成功");
        return resMap;
    }

    @Override
    public Map<String, Object> delConsumer(Consumer consumer) {
        Map<String, Object> resMap = ResMap.OK();
        this.removeById(consumer.getId());
        resMap.put("msg", "删除成功");
        return resMap;
    }
}
